package com.lingnan.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lingnan.pojo.Active;
import com.lingnan.pojo.Receive;
import com.lingnan.pojo.User;
import com.lingnan.service.ActiveService;
import com.lingnan.service.ReceiveService;
import com.lingnan.service.TargetService;
import com.lingnan.service.UserService;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.ReceiveVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * <p>
 * 收到表 前端控制器
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@RestController
@CrossOrigin
@RequestMapping("/receive")
public class ReceiveController {

    @Autowired
    ReceiveService receiveService;

    @Autowired
    UserService userService;

    @Autowired
    TargetService targetService;

    @Autowired
    ActiveService activeService;

    @PostMapping("/reply")
    @ApiOperation("紧急通知收到")
    public Result reply(@RequestBody @ApiParam("传学号no和紧急通知id和时间") Receive receive){
        if (!ObjectUtils.isEmpty(receive.getNo()) && !ObjectUtils.isEmpty(receive.getInfoId())){
            Receive byNoAndInfoId = receiveService.findByNoAndInfoId(receive);
            if (byNoAndInfoId == null){
                boolean save = receiveService.save(receive);
                UpdateWrapper<Active> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("is_receive",1);
                updateWrapper.eq("id",receive.getInfoId());
                activeService.update(updateWrapper);
                if (save){
                    return ResultGenerator.genSuccessResult(200, "用户回复收到成功！");
                }
            }
            return ResultGenerator.genErrorResult(201, "该用户已经回复收到，请勿重复回复收到");
        }
        return ResultGenerator.genErrorResult(201, "参数错误，用户回复收到失败");
    }

    @PostMapping("/findAllReply")
    @ApiOperation("查询某个活动已经点击收到的学生")
    public Result findAllReply(@RequestBody @ApiParam("传活动ID") ReceiveVo receiveVo){
        if (!ObjectUtils.isEmpty(receiveVo)){
            List<User> userList = receiveService.findReplyUserByInfoId(receiveVo.getInfoId());
            return ResultGenerator.genSuccessResult(200, userList, "查询成功");
        }
        return ResultGenerator.genSuccessResult(201, "查询失败");
    }

    @PostMapping("/findAllNoReply")
    @ApiOperation("查询某个活动未点击收到的学生")
    public Result findAllNoReply(@RequestBody @ApiParam("传活动ID") ReceiveVo receiveVo){
        if (!ObjectUtils.isEmpty(receiveVo)){
            List<User> userList = receiveService.findNoReplyUserByInfoId(receiveVo.getInfoId());
            return ResultGenerator.genSuccessResult(200, userList, "查询成功");
        }
        return ResultGenerator.genSuccessResult(201, "查询失败");
    }
}

